﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using Sherwood.Security;
using Sherwood.SignOn.Server;
using Sherwood.SignOn.Server.Models;
using Sherwood.SignOn.Server.Models.Repositories;
using Sherwood.SignOn;
using Sherwood.SignOn.Server.Models.Services;

namespace Sherwood.SignOn.Server.Models.Repositories.InMemory
{
    public class UserSessionRepository : IUserSessionRepository
    {


        public UserSessionRepository()
        {

        }


        //In-memory user session store
        private List<UserSession> _all = new List<UserSession>();

        /// <summary>
        /// Returns all user sessions
        /// </summary>
        public virtual List<UserSession> All
        {
            get
            {
                return _all;                
            }
        }


        private List<UserSessionClientRecord> _clientRecords = new List<UserSessionClientRecord>();
        public virtual List<UserSessionClientRecord> ClientRecords
        {
            get
            {
                return _clientRecords;
            }
        }
        
        public void RemoveUserSession(Guid signOnSessionId)
        {
            UserSession session = GetUserSession(signOnSessionId) as UserSession;
            _all.Remove(session);
        }

        public IUserSession AddUserSession(Guid userAccountId, string ipAddress, bool persistent)
        {
            UserSession session = new UserSession();
            session.UserAccountId = userAccountId;
            session.Id = Guid.NewGuid();
            session.DateCreated = DateTime.Now;
            session.IpAddress = ipAddress;
            session.Persistent = persistent;
            _all.Add(session);
            return session;
        }

        public IUserSession GetUserSession(Guid userSessionId)
        {
            return (from s in All where s.Id.Equals(userSessionId) select s).FirstOrDefault();
        }

        private static int _nextClientRecordId = 1;

        public void AddUserSessionClientRecord(Guid userSessionId, string clientSessionId, string signOutUrl)
        {
            UserSession session = Data.UserSessions.GetUserSession(userSessionId) as UserSession;
            if (session != null)
            {
                UserSessionClientRecord clientRecord = (from r in session.ClientRecords
                                                        where
                                                            r.ClientSessionId != null &&
                                                            r.ClientSessionId.Equals(clientSessionId) &&
                                                            r.SignOutUrl != null && r.SignOutUrl.Equals(signOutUrl)
                                                        select r).FirstOrDefault();
                if (clientRecord == null) //if client hasn't already been recorded
                {
                    clientRecord = new UserSessionClientRecord();
                    clientRecord.Id = ++_nextClientRecordId;
                    clientRecord.UserSessionId = session.Id;
                    clientRecord.ClientSessionId = clientSessionId;
                    clientRecord.SignOutUrl = signOutUrl;
                    session.ClientRecords.Add(clientRecord);
                }
            }
        }

 
    }
}
